//
//  WSBaseViewController.m
//  FamilyLinkPro
//
//  Created by 江鹏 on 2023/9/5.
//  Copyright © 2023 SiterWell.Co.,Ltd. All rights reserved.
//

#import "WSBaseViewController.h"

@interface WSBaseViewController ()

@end

@implementation WSBaseViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WhiteColor;
    self.hbd_barAlpha = 0.0;
    self.hbd_barStyle = UIBarStyleDefault;
    self.hbd_tintColor = UIColor.blackColor;
    self.hbd_barShadowHidden = YES;
    self.title = @"";
    
    // TODO: 当前控制器最前面的一个不存在返回的按钮.
    if (self.navigationController.viewControllers.count > 1) {
        [self configLeftBarButtonItem];
    }
}

- (void)setIsHideLeftBarButtonItem:(BOOL)isHideLeftBarButtonItem {
    if (!isHideLeftBarButtonItem) {
        [self configLeftBarButtonItem];
    }
}


- (void)configLeftBarButtonItem {
    UIImage *leftImageName = [UIImage imageNamed:@"iv_hvac_back"];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, leftImageName.size.width, 40)];
    [backBtn setImage:leftImageName forState:UIControlStateNormal];
    [backBtn setImage:leftImageName forState:UIControlStateHighlighted];
    [backBtn setShowsTouchWhenHighlighted:NO];
    [backBtn addTarget:self action:@selector(pb_backToPreviousViewController) forControlEvents:UIControlEventTouchUpInside];
    if (_ios11_0_){
        [backBtn setFrame:CGRectMake(0, 0, 40, 40)];
        backBtn.contentEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 10);
    }
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = item;
}

- (void)pb_backToPreviousViewController {
    if (self.navigationController.presentingViewController){
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}



- (void)setNavigationBarAlpha:(CGFloat)alpha {
    if (self.navigationController) {
        [self.navigationController.navigationBar setBackgroundImage:[self drawPngImageWithAlpha:alpha] forBarMetrics:(UIBarMetricsDefault)];
    }
}


#pragma mark - Private Methods

- (UIImage *)drawPngImageWithAlpha:(CGFloat)alpha {
    UIColor *color = BlackColor;
    CGSize size = CGSizeMake(1, 1);
    UIGraphicsBeginImageContext(size);
    CGContextRef content = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(content, color.CGColor);
    CGContextFillRect(content, CGRectMake(0, 0, 1, 1));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)setIsHiddenNavigationBarBottomBorder:(BOOL)isHiddenNavigationBarBottomBorder {
    _isHiddenNavigationBarBottomBorder = isHiddenNavigationBarBottomBorder;
    if (self.isHiddenNavigationBarBottomBorder) {
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    } else {
        [self.navigationController.navigationBar setShadowImage:self.navigationBarOriginShadowImage];
    }
}




@end
